import {
  getContentTable,
  editContentRecord
} from '../../api/content'
import {
  H5_LIST,
  H5_LIST_CURRENT_ROW,
  H5_LIST_DATA_COUNT,
  H5_LIST_HISTORY_DATA,
  IMG_HISTORY_DATA,
  H5_LIST_PAGE_SIZE
} from '../motations-type'
import qqgroup from '@/assets/images/qq-group1.jpg'

export default {
  state: {
    h5List: [],
    h5DataCount: null,
    h5PageSize: 20,
    imgPageSize: 8,
    imgHistoryData: [],
    h5HistoryData: [],
    row: {
      url: '',
      ctime: '',
      title: '',
      updateTime: ''
    }
  },
  getters: {
    h5List: state => state.h5List
  },
  mutations: {
    [H5_LIST] (state, data) {
      state.h5List = data
    },
    [H5_LIST_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [H5_LIST_DATA_COUNT] (state, data) {
      state.h5DataCount = data
    },
    [H5_LIST_HISTORY_DATA] (state, data) {
      state.h5HistoryData = data
    },
    [IMG_HISTORY_DATA] (state, data) {
      state.imgHistoryData = data
    },
    [H5_LIST_PAGE_SIZE] (state, data) {
      state.h5PageSize = data
    }
  },
  actions: {
    async getH5List ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getContentTable(data)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [{
          id: item.id,
          type: item.type,
          img: item.img,
          host: host,
          ctime: item.ctime,
          name: item.name,
          uptime: item.uptime,
          content: item.content
        }, ...newData]
      })
      commit(H5_LIST, newData)
      commit(H5_LIST_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.h5Content.h5PageSize) {
        commit(H5_LIST_HISTORY_DATA, newData)
      } else {
        commit(H5_LIST_HISTORY_DATA, newData.slice(0, rootState.h5Content.h5PageSize))
      }
      if (newData.length < rootState.h5Content.imgPageSize) {
        commit(IMG_HISTORY_DATA, newData)
      } else {
        commit(IMG_HISTORY_DATA, newData.slice(0, rootState.h5Content.imgPageSize))
      }
    },
    async editH5ListData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      await editContentRecord(data)
    },
    async getH5ListCurrentRow ({ commit, rootState }, row) {
      commit(H5_LIST_CURRENT_ROW, row)
    },
    async changeH5ListPage ({ commit, rootState }, index) {
      let data = rootState.h5Content.h5List
      let _start = (index - 1) * rootState.h5Content.h5PageSize
      let _end = index * rootState.h5Content.h5PageSize
      commit(H5_LIST_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeH5ListPageSize ({ commit, rootState }, value) {
      commit(H5_LIST_PAGE_SIZE, value)
    },
    async changeImgPage ({ commit, rootState }, index) {
      let data = rootState.h5Content.h5List
      let _start = (index - 1) * rootState.h5Content.imgPageSize
      let _end = index * rootState.h5Content.imgPageSize
      commit(IMG_HISTORY_DATA, data.slice(_start, _end))
    }
  }
}
